//
//  LoginViewController.m
//  QYer
//
//  Created by lanou on 16/8/8.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"

#import "UMSocial.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *usernameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.usernameTF.delegate = self;
    self.usernameTF.tag = 200;
    self.passwordTF.delegate = self;
    self.passwordTF.tag = 201;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//返回
- (IBAction)backToVC:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//登录
- (IBAction)usernameLoginAction:(id)sender {
    NSLog(@"登录");
}

//手机号登录
- (IBAction)phoneNumberLoginAction:(id)sender {
    NSLog(@"手机号登录");
}

//忘记密码
- (IBAction)forgetPasswordAction:(id)sender {
    NSLog(@"忘记密码");
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
*/


#pragma mark ----- 第三方登录 -----

//新浪微博登录
- (IBAction)loginBySinaWeibo:(id)sender {
    /*
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        //获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            NSLog(@"%@", dict);
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
        }
    });
    */
}

//微信登录
- (IBAction)loginByWeChat:(id)sender {
    /*
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            NSLog(@"%@", dict);
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            
            
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    });
    */
}

//QQ登录
- (IBAction)loginByQQ:(id)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    
    //loginClickHandler 处理点击事件
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //获取微博用户名、uid、token等
        //responseCode 发送结果-->成功
        if (response.responseCode == UMSResponseCodeSuccess) {
            //NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            //NSLog(@"%@", dict);
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            NSLog(@"登录成功!");
            //登陆成功保存用户信息
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:snsAccount.userName forKey:@"username"];
            [userDefaults setObject:snsAccount.usid forKey:@"usid"];
            [userDefaults setObject:snsAccount.accessToken forKey:@"token"];
            [userDefaults synchronize];
            //登陆界面消失
            [self dismissViewControllerAnimated:YES completion:nil];
            //NSLog(@"AAA: %@", snsAccount.userName);
            //NSLog(@"保存的用户名: %@", [[NSUserDefaults standardUserDefaults] objectForKey:@"username"]);
            
            //NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            
        }
    });
}


#pragma mark ----- UITextFieldDelegate -----

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 200) {
        [self.usernameTF resignFirstResponder];
        [self.passwordTF becomeFirstResponder];
    } else {
        [self.passwordTF resignFirstResponder];
        [self usernameLoginAction:nil];
    }
    return YES;
}



@end
